package com.rentapark.app;

import static data.CommonUtilities.SENDER_ID;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

import com.google.android.gcm.GCMBaseIntentService;

import data.DB;

/*this service handles GCM events*/
public class GCMIntentService extends GCMBaseIntentService{
	DB parking_db;
	private static final String TAG = "===GCMIntentService===";
	private NotificationManager notificationMgr;
	
	/*GCMIntentService constructor*/
	public GCMIntentService() {
		super(SENDER_ID);
	}


	/*onRegistered event*/
	protected void onRegistered(Context arg0, String registrationId) {
		Log.i(TAG, "Device registered: regId = " + registrationId);
	
	}

	/*onUnRegistered event*/
	protected void onUnregistered(Context arg0, String arg1) {
		Log.i(TAG, "unregistered = "+arg1);
	}

	/*onMessage event*/
	protected void onMessage(Context arg0, Intent arg1) {
		parking_db=DB.getInstance();
		notificationMgr =(NotificationManager)getSystemService(
				NOTIFICATION_SERVICE);
		Log.i(TAG, "new message= i love you!!!");
		Log.d("onMessage",arg1.getExtras().getString("message"));
		String data=arg1.getExtras().getString("message");
		if(data.equals("0"))//this notification informs the spot's owner that he got a request
		{
			parking_db.notification_type=0;//specify notification type
			Intent dialogIntent = new Intent(getBaseContext(), NotificationHandler.class);
			dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			getApplication().startActivity(dialogIntent);//start activity NotificationHandler
		}
		else if(data.equals("1"))//this notification informs the buyer that th spot's owner confirmed his request
		{
			parking_db.notification_type=1;
			Log.d("onMessage2",arg1.getExtras().getString("spot_id"));
			String spotId=arg1.getExtras().getString("spot_id");
			parking_db.spotRentId=Integer.parseInt(spotId);
			Intent dialogIntent = new Intent(getBaseContext(), NotificationHandler.class);
			dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			getApplication().startActivity(dialogIntent);
			
		}
		else if (data.equals("2"))//this notification notifies both buyer and owner that the deal has been confirme
		{
			parking_db.notification_type=2;
			
			Intent dialogIntent = new Intent(getBaseContext(), NotificationHandler.class);
			dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			getApplication().startActivity(dialogIntent);
		}
		/*
		 * //this notification informs the spot's owner that the deal confirmation hasn't been
		 * complited: the candidate rejected the deal
		 */
		else if (data.equals("3"))
		{
			parking_db.notification_type=3;
			
			Intent dialogIntent = new Intent(getBaseContext(), NotificationHandler.class);
			dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			getApplication().startActivity(dialogIntent);
		}
		else//other notification
		{
			displayNotificationMessage(data);
			
		}
			
		Log.d("db-not type is: ",data);
		//check which kind of message arrived
		
	}
	//method for displaying notification in notification bar
	private void displayNotificationMessage(String message)
	{
		Notification notification = new Notification(R.drawable.alert,
				message,System.currentTimeMillis());
		PendingIntent contentIntent =
				PendingIntent.getActivity(this, 0,new Intent(this, MainScreen.class), 0);
		notification.setLatestEventInfo(this, "Rent-A-Park",message,
				contentIntent);
		notificationMgr.notify(R.string.app_notification_id, notification);
	}

	/*onError event*/
	protected void onError(Context arg0, String errorId) {
		Log.i(TAG, "Received error: " + errorId);
	}

	/*onRecoverableError event*/
	protected boolean onRecoverableError(Context context, String errorId) {
		return super.onRecoverableError(context, errorId);
	}
}


